package com.spzx.product.service.impl;


import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.spzx.common.core.exception.ServiceException;
import com.spzx.common.core.web.page.PageDomain;
import com.spzx.common.core.web.page.TableSupport;
import com.spzx.common.security.utils.SecurityUtils;
import com.spzx.product.domain.Category;
import com.spzx.product.domain.CategoryBrand;
import com.spzx.product.domain.vo.CategoryBrandVO;
import com.spzx.product.mapper.BrandMapper;
import com.spzx.product.mapper.CategoryBrandMapper;
import com.spzx.product.mapper.CategoryMapper;
import com.spzx.product.service.CategoryBrandService;
import jakarta.annotation.Resource;
import org.apache.catalina.security.SecurityUtil;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

@Service
public class CategoryBrandServiceImpl extends ServiceImpl<CategoryBrandMapper, CategoryBrand> implements CategoryBrandService {
    @Resource
    CategoryBrandMapper categoryBrandMapper;

    @Resource
    CategoryMapper categoryMapper;

    @Override
    public Page<CategoryBrandVO> selectList(CategoryBrand categoryBrand) {
        PageDomain pageDomain = TableSupport.getPageDomain();

        Page<CategoryBrandVO> page = new Page<>(pageDomain.getPageNum(), pageDomain.getPageSize());


        Page<CategoryBrandVO>  page1 =   categoryBrandMapper.selectCategoryBrandVOList(page,categoryBrand);



        return page;
    }

    @Override
    public CategoryBrand selectCategoryBrandById(Long id) {

        CategoryBrand categoryBrand = categoryBrandMapper.selectOne(new LambdaQueryWrapper<CategoryBrand>().eq(CategoryBrand::getId,id));
        /*ArrayList<Long> ids = new ArrayList<>();
        //3
        Long id3 = categoryBrand.getCategoryId();
        Long parentId = categoryMapper.selectOne(new LambdaQueryWrapper<Category>().eq(Category::getId, id3)).getParentId();
        //2
        Category category2 = categoryMapper.selectOne(new LambdaQueryWrapper<Category>().eq(Category::getId, parentId));
        Long id2 = category2.getParentId();
        //1
        Category category1 = categoryMapper.selectOne(new LambdaQueryWrapper<Category>().eq(Category::getId, id2));
        Long id1 = category1.getId();

        ids.add(id1);
        ids.add(category2.getId());
        ids.add(id3);*/
        List<Long> ids = select3CategoryId(categoryBrand.getCategoryId());

        categoryBrand.setCategoryIdList(ids);
        return categoryBrand;
    }


    @Override
    public List<Long> select3CategoryId(Long categoryId){
        List<Long> ids = new ArrayList<>();
        //3
        Long id3 = categoryId;
        Long parentId = categoryMapper.selectOne(new LambdaQueryWrapper<Category>().eq(Category::getId, id3)).getParentId();
        //2
        Category category2 = categoryMapper.selectOne(new LambdaQueryWrapper<Category>().eq(Category::getId, parentId));
        Long id2 = category2.getParentId();
        //1
        Category category1 = categoryMapper.selectOne(new LambdaQueryWrapper<Category>().eq(Category::getId, id2));
        Long id1 = category1.getId();

        ids.add(id1);
        ids.add(category2.getId());
        ids.add(id3);

        return ids;
    }



    @Override
    public void checkUniqueCategoryBrand(CategoryBrand categoryBrand) {
        Long id = categoryBrand.getId()==null?-1:categoryBrand.getId();

        CategoryBrand dbCategoryBrand = categoryBrandMapper.selectOne(new LambdaQueryWrapper<CategoryBrand>()
                .eq(CategoryBrand::getCategoryId, categoryBrand.getCategoryId())
                .eq(CategoryBrand::getBrandId, categoryBrand.getBrandId()));

        if (dbCategoryBrand!=null && !id.equals(dbCategoryBrand.getId())){
            throw new ServiceException("分类品牌信息已存在");
        }

    }
}
